<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->

<template>
  <div class="execution">
    <basic-container>
      <avue-crud ref="crud"
                 :page="page"
                 :data="tableData"
                 :table-loading="tableLoading"
                 :option="tableOption"
                 @on-load="getList"
                 @search-change="searchChange"
                 @refresh-change="refreshChange"
                 @row-update="handleUpdate"
                 @row-save="handleSave"
                 @row-del="rowDel">
        <template slot="menuLeft">
          <el-button type="primary"
                     @click="myExport"
                     size="small"
                     v-if="permissions.antborrow_filecustomer_export">导出</el-button>
          <br /><br />
        </template>
        <template slot-scope="scope"
                  slot="menu">
          <el-button type="text"
                     v-if="permissions.antborrow_filecustomer_detail"
                     icon="el-icon-check"
                     size="small"
                     plain
                     @click="showHistory(scope.row)">查看授信历史</el-button>
        </template>
      </avue-crud>
    </basic-container>

    <!--历史授信信息表格-->
    <el-dialog title="历史授信" width="80%" v-if="dialogTableVisible" :visible.sync="dialogTableVisible">
      <el-table border size="mini" :data="gridData" >
        <el-table-column align="center" property="username" label="用户姓名" width="150"></el-table-column>
        <el-table-column align="center" property="phone" label="手机号" width="200"></el-table-column>
        <el-table-column align="center" property="identityNo" label="证件号"></el-table-column>
        <el-table-column align="center" property="validityDate" label="证件期效"><template slot-scope="scope">{{scope.row.validityDate | dateFormat}}</template></el-table-column>
        <el-table-column align="center" property="province" label="省"></el-table-column>
        <el-table-column align="center" property="city" label="市"></el-table-column>
        <el-table-column align="center" property="area" label="区"></el-table-column>
        <el-table-column align="center" property="addr" label="地址"></el-table-column>
        <el-table-column align="center" property="jobType" label="职业类型"></el-table-column>
        <el-table-column align="center" property="nationality" label="国籍"></el-table-column>
        <el-table-column align="center" property="workUnit" label="工作单位"></el-table-column>
        <el-table-column align="center" property="quotaAmt" label="额度"><template slot-scope="scope">{{scope.row.quotaAmt | amtFormat}}</template></el-table-column>
        <el-table-column align="center" property="interestRate" label="利率"></el-table-column>
        <el-table-column align="center" property="creditDate" label="授信日期"><template slot-scope="scope">{{scope.row.creditDate | dateFormat}}</template></el-table-column>
        <el-table-column align="center" property="validFlag" label="是否有效"><template slot-scope="scope">{{scope.row.validFlag?"有效":"无效"}}</template></el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
  import Tool from "@/util/Tool";
  import { fetchList, getObj, addObj, putObj, delObj, detail, exportExcel } from '@/api/antborrow/filecustomer'
  import { tableOption } from '@/const/crud/antborrow/filecustomer'
  import { mapGetters } from 'vuex'
  export default {
    name: 'filecustomer',
    data() {
      return {
        gridData: '',
        dialogTableVisible: false,
        tableData: [],
        page: {
        },
        tableLoading: false,
        tableOption: tableOption,
      }
    },
    created() {
    },
    mounted: function() { },
    computed: {
      ...mapGetters(['permissions'])
    },
    filters:{
      amtFormat(val){
        if (val){
          return (val / 100).toFixed(2);
        }
      },
      dateFormat(val){
        if(val){
          return val.toString().substring(0,10);
        }
      }
    },
    methods: {
      getList(page) {
        this.tableLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, this.queryParams)).then(response => {
          let arr = response.data.data.records;
          arr.map(item => {
            item.quotaAmt = this.moneySwitch(item.quotaAmt)
            item.identityNo = this.displayIdCard(item.identityNo, 5, 3)
            item.phone = this.displayIdCard(item.phone, 4, 3)
          });
            this.tableData = response.data.data.records
            this.page.pageSize = response.data.data.pageSize
            this.page.total = response.data.data.total
            this.tableLoading = false
        })
      },
      moneySwitch: function moneySwitch(val) {
        return val.toFixed(2);
      },
      displayIdCard: function plusXing(str, frontLen, endLen) {
        if (str != null && str !== "") {
          let len = str.length - frontLen - endLen;
          let xing = '';
          for (let i = 0; i < len; i++) {
            xing += '*';
          }
          return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
        }
      },
      showHistory(row) {
          detail(row.id).then(res => {
            this.dialogTableVisible = true
            this.gridData = res.data.data
          });
      },
      myExport() {
        exportExcel(this.queryParams).then(res => {
          if (!res) {
            return
          }
          let blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf-8'});
          let link = document.createElement('a');
          link.download = Tool.formattime2(new Date()).toString() + '_UserCredit.xlsx';
          link.style.display = 'none';
          link.href = URL.createObjectURL(blob);
          document.body.appendChild(link);
          link.click();
          URL.revokeObjectURL(link.href);
          document.body.removeChild(link);
        });
      },
      /**
       **/
      handleAdd: function() {
        this.$refs.crud.rowAdd()
      },
      handleEdit(row, index) {
        this.$refs.crud.rowEdit(row, index)
      },
      handleDel(row, index) {
        this.$refs.crud.rowDel(row, index)
      },
      rowDel: function(row, index) {
        var _this = this
        this.$confirm('是否确认删除ID为' + row.id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
            return delObj(row.id)
          }).then(data => {
          _this.tableData.splice(index, 1)
          _this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
          this.getList(this.page)
        }).catch(function(err) { })
      },
      /**
       **/
      handleUpdate: function(row, index, done) {
        putObj(row).then(data => {
          this.tableData.splice(index, 1, Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
          done()
          this.getList(this.page)
        })
      },
      /**
       **/
      handleSave: function(row, done) {
        addObj(row).then(data => {
          this.tableData.push(Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
          done()
          this.getList(this.page)
        })
      },
      /**
       */
      searchChange(form) {
          this.queryParams = this.filterForm(form)
          this.getList(this.page)
      },
      /**
       */
      refreshChange() {
        this.getList(this.page)
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
